﻿namespace Trader.BO
{
    using System;
    using Interface;

    public class Money : IMoney
    {
        private readonly ICurrecny currecny;
        private decimal value;

        public Money(decimal value, ICurrecny currecny)
        {
            this.value = value;
            this.currecny = currecny;
        }

        public override string ToString()
        {
            return string.Format("{0:F2}{1}", value, currecny.Sign);
        }

        public override IMoney Clone()
        {
            return new Money(value, currecny);
        }

        public override void Add(IMoney money, DateTime date)
        {
            value += money.In(currecny, date);
        }

        public override void Substract(IMoney money, DateTime date)
        {
            value -= money.In(currecny, date);
        }

        public override void Multiply(decimal ratio)
        {
            value *= ratio;
        }

        public override decimal In(ICurrecny currencyToConvertInto, DateTime date)
        {
            decimal ratio;
            if (currencyToConvertInto == currecny)
            {
                ratio = 1;
            }
            else
            {
                ratio = currecny.ConvertTo(currencyToConvertInto, date);
            }

            return value*ratio;
        }

        public override decimal In()
        {
            return value;
        }
    }
}